package 栈.练习;
//括号匹配问题
import java.util.Stack;

public class Text_1 {
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        //取出字符串中的每个元素
        for(int i = 0;i<s.length();i++){
            char ch = s.charAt(i);
            //进行判断，分为两边，左边和右边
            if(ch == '(' || ch == '{'||ch =='['){
                stack.push(ch);
            }else {
                //属于右边
                if(stack.empty()){
                   return false;
                }else {
                    char top = stack.peek();
                    if(top =='('&&ch ==')'||top =='['&&ch ==']'||top =='{'&&ch =='}'){
                        stack.pop();
                    }
                    return false;
                }
            }
        }
        //遍历完，看是否有剩余
        if(!stack.empty()){
            return false;
        }
        return true;
    }
}
